Skip to content

fix(postgrest): fix scalar computed column type inference for isNotNullable and SETOF scalar - #2224

Merged
mandarini merged 2 commits into
masterfrom
fix/postgrest-scalar-type
Apr 7, 2026
Merged

mandarini merged 2 commits into
masterfrom
fix/postgrest-scalar-type

Conversation

@mandarini

@mandarini mandarini commented Apr 6, 2026 •

Copy link
Copy Markdown
Contributor

Description

What changed?

Fixes two type inference bugs for scalar computed columns — PostgREST functions used as embedded fields in .select() that return a primitive value instead of a table row. Also fixes a regression introduced during the fix where regular table embeds with empty parens (e.g. users()) were incorrectly added to the result type.

Bug 1 — non-SETOF non-nullable scalar (RETURNS bigint NOT NULL)

When SetofOptions.to is '' (no target table), TablesAndViews<Schema>[''] resolves to never, so ProcessNodes produces {} instead of the correct primitive type (e.g. number).

Bug 2 — SETOF scalar (RETURNS SETOF uuid)

Same root cause: to: '' causes the table lookup to return never, so the final type resolves to never[] instead of string[].

Regression fix — empty-parens table embed (users())

While routing empty () through ProcessEmbeddedResource to handle scalar columns, regular table embeds with no fields selected (e.g. users()) were incorrectly gaining a users: {} key in the result type. The prior behavior (no contribution to the result type) is now restored.

Changes:

  • utils.ts: ResolveEmbededFunctionJoinTableRelationship now returns the full GenericFunction (not just SetofOptions) so Fn['Returns'] is available downstream. ResolveForwardRelationship branches on SetofOptions['to'] extends '': scalar functions build a resolved object with a scalarType property set to Fn['Returns']; table-valued functions use the existing logic unchanged.
  • result.ts: ProcessFieldNode routes empty () through ProcessEmbeddedResource instead of immediately returning {}. ProcessEmbeddedResource gains a scalar early-exit: if the resolved object has scalarType, the primitive type is returned with correct nullability (ScalarType for isNotNullable: true or isSetofReturn: true, ScalarType | null otherwise). For regular table embeds with empty children, {} is returned to preserve prior behavior.
  • test/types.generated.ts: two new scalar function fixtures (scalar_computed_count with to: '', isNotNullable: true; scalar_computed_ids with to: '', isSetofReturn: true).
  • test/embeded_functions_join.test.ts: type assertion tests verifying scalar_computed_count resolves to number and scalar_computed_ids resolves to string[].

Why was this change needed?

Scalar computed columns were previously typed incorrectly: non-nullable scalars got {} and SETOF scalars got never[]. This fix makes the SDK type-safe for scalar computed columns once the Supabase CLI generates SetofOptions with to: '' for these functions (a follow-up CLI change is needed).

The fix is SDK-only. PostgREST returns correct data at runtime. Closes #1818

Screenshots/Examples

Before:

// RETURNS bigint NOT NULL
.from('comments').select('*, user_count()')
// user_count: {}  ← wrong

// RETURNS SETOF uuid
.from('course').select('*, progress_user()')
// progress_user: never[]  ← wrong

// Regular embed with no fields
.from('messages').select('message, users()')
// { message: string | null, users: {} }  ← wrong (regression)

After:

// RETURNS bigint NOT NULL
.from('comments').select('*, user_count()')
// user_count: number  ← correct

// RETURNS SETOF uuid
.from('course').select('*, progress_user()')
// progress_user: string[]  ← correct

// Regular embed with no fields
.from('messages').select('message, users()')
// { message: string | null }  ← correct

Breaking changes

  • This PR contains no breaking changes

Checklist

  • I have read the Contributing Guidelines
  • My PR title follows the conventional commit format: <type>(<scope>): <description>
  • I have run npx nx format to ensure consistent code formatting
  • I have added tests for new functionality (if applicable)
  • I have updated documentation (if applicable)

Additional notes

The scalar computed column fix activates fully once the CLI generates SetofOptions with to: '' for scalar computed columns. Until then, the new code path is exercised only by the test fixtures added in this PR.

@mandarini
mandarini requested review from a team as code owners April 6, 2026 16:06
@github-actions github-actions Bot added the postgrest-js Related to the postgrest-js library. label Apr 6, 2026
@pkg-pr-new

pkg-pr-new Bot commented Apr 6, 2026 •

Copy link
Copy Markdown

Open in StackBlitz

@supabase/auth-js

npm i https://pkg.pr.new/@supabase/auth-js@2224

@supabase/functions-js

npm i https://pkg.pr.new/@supabase/functions-js@2224

@supabase/postgrest-js

npm i https://pkg.pr.new/@supabase/postgrest-js@2224

@supabase/realtime-js

npm i https://pkg.pr.new/@supabase/realtime-js@2224

@supabase/storage-js

npm i https://pkg.pr.new/@supabase/storage-js@2224

@supabase/supabase-js

npm i https://pkg.pr.new/@supabase/supabase-js@2224

commit: b4b048f

@mandarini
mandarini force-pushed the fix/postgrest-scalar-type branch from 81f293e to 547351d Compare April 6, 2026 16:36
@mandarini
mandarini force-pushed the fix/postgrest-scalar-type branch from 547351d to b4b048f Compare April 7, 2026 12:07
@mandarini mandarini self-assigned this Apr 7, 2026

@avallete avallete left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good to me !

@mandarini
mandarini merged commit d86463f into master Apr 7, 2026
24 checks passed
@mandarini
mandarini deleted the fix/postgrest-scalar-type branch April 7, 2026 14:10
mandarini pushed a commit to supabase/supabase that referenced this pull request Apr 9, 2026
This PR updates @supabase/*-js libraries to version 2.103.0.

**Source**: supabase-js-stable-release

**Changes**:
- Updated @supabase/supabase-js to 2.103.0
- Updated @supabase/auth-js to 2.103.0
- Updated @supabase/realtime-js to 2.103.0
- Updated @supabase/postgest-js to 2.103.0
- Refreshed pnpm-lock.yaml

---

## Release Notes

## v2.103.0

## 2.103.0 (2026-04-09)

### 🚀 Features

- **postgrest:** add stripNulls method for null value stripping
([#2189](supabase/supabase-js#2189))
- **storage:** add cacheNonce parameter for download
([#2234](supabase/supabase-js#2234))

### 🩹 Fixes

- **postgrest:** fix scalar computed column type inference for
isNotNullable and SETOF scalar
([#2224](supabase/supabase-js#2224))

### ❤️ Thank You

- Katerina Skroumpelou @mandarini
- Seydi Charyyev @TheSeydiCharyyev
- Vaibhav @7ttp

This PR was created automatically.

Co-authored-by: supabase-workflow-trigger[bot] <266661614+supabase-workflow-trigger[bot]@users.noreply.github.com>
mandarini pushed a commit to supabase/ssr that referenced this pull request Apr 9, 2026
This PR updates `@supabase/supabase-js` to v2.103.0.

**Source**: supabase-js-stable-release

---

## Release Notes

## v2.103.0

## 2.103.0 (2026-04-09)

### 🚀 Features

- **postgrest:** add stripNulls method for null value stripping
([#2189](supabase/supabase-js#2189))
- **storage:** add cacheNonce parameter for download
([#2234](supabase/supabase-js#2234))

### 🩹 Fixes

- **postgrest:** fix scalar computed column type inference for
isNotNullable and SETOF scalar
([#2224](supabase/supabase-js#2224))

### ❤️ Thank You

- Katerina Skroumpelou @mandarini
- Seydi Charyyev @TheSeydiCharyyev
- Vaibhav @7ttp

This PR was created automatically.

Co-authored-by: supabase-workflow-trigger[bot] <266661614+supabase-workflow-trigger[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

postgrest-js Related to the postgrest-js library.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Non nullable computed field not properly introspected

3 participants